package com.graduate.front.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.graduate.common.exception.UserException;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author bwzfy
 * @create 2021/5/11
 **/
@Aspect
//@Component
@Slf4j
public class LoginAop {

    @Pointcut("execution(public * com.graduate.front.controller..*.*(..))")
    public void pointCut() {

    }

    /**
     * session过期问题
     */
    @Around("pointCut()")
    public Object sessionOverdue(ProceedingJoinPoint joinPoint) {
        log.info("开始进行session判断");
        try {
//            // 从request的上下文中获取HttpRequest对象
//            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//            //从获取RequestAttributes中获取HttpServletRequest的信息
//            HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//            HttpSession session = request.getSession();
//            Object user = session.getAttribute("user");
//            if (user == null) {
//                // 如果没有从session中获取到用户，那就说明当前没有用户登录或者登录过期了
//                throw new UserException("user session");
//            }
            // 需要执行的目标方法
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }
}
